package com.wn.controller;

import com.wn.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author yellow Docter
 * @date 2023 -02-20
 * @desc
 */
@RequestMapping("member")
@RestController
@Slf4j
@CrossOrigin
public class MemberController {

    @Value("${server.port}")
    private Integer port;

    @GetMapping ("{id}")
    public String getMemberById(@PathVariable("id") Integer id) {

        log.info("提供服务:{}",id);
        log.info("服务端口是：{}",port);
        return "通过id拿到member:" + id + "," + port;
    }

    @GetMapping ("/{id}/{name}")
    public String getMemberByIdAndName(
            @PathVariable("id") Integer id,
            @PathVariable("name") String name) {
        log.info("提供服务:{}",id);
        log.info("提供服务:{}",name);
        return "通过id拿到member:id=" + id + ",name="+name;
    }

    /**
     * @RequestBody   把请求体中的json 转成对象
     *
     * @param map
     * @return
     */
    @PostMapping
    public String submitData(@RequestBody HashMap<String,Object> map) {

        System.out.println("map = " + map);
        Integer id = (Integer) map.get("id");
        String name = (String) map.get("name");

        log.info("提供服务id:{}",id);
        log.info("提供服务name:{}",name);
        return "通过id拿到member:id=" + id + ",name="+name;
    }

    @GetMapping ("/get/{name}/{id}")
    public User getMember(
            @PathVariable("name") String name,
            @PathVariable("id") String id) {
        return User.builder().id(id).name(name).build();
    }

    @PostMapping ("/user")
    public User getMember(@RequestBody User user) {
        return user;
    }

}